//
//  WMBaseTabBarViewController.m
//  WaiMai
//
//  Created by Angela on 2017/8/24.
//  Copyright © 2017年 Angela. All rights reserved.
//

#import "WMBaseTabBarViewController.h"
#import "WMHomeViewController.h"
#import "WMOrderViewController.h"
#import "WMFindViewController.h"
#import "WMMineViewController.h"
@interface WMBaseTabBarViewController ()<UITabBarControllerDelegate,UITabBarDelegate>
@property (nonatomic, strong) WMBaseNavViewController *homeVC;
@property (nonatomic, strong) WMBaseNavViewController *orderVC;
@property (nonatomic, strong) WMBaseNavViewController *findVC;
@property (nonatomic, strong) WMBaseNavViewController *mineVC;
@end

@implementation WMBaseTabBarViewController
-(WMBaseNavViewController *)selectedViewController{
    return (WMBaseNavViewController *)[super selectedViewController];
}
-(instancetype)init{
    self=[super init];
    if (self) {
        self.fd_interactivePopDisabled=YES;
        self.fd_prefersNavigationBarHidden=YES;
        [self makeTabBarHigh];
        WMHomeViewController *homeVC=[[WMHomeViewController alloc]init];
        self.homeVC=[self addOneChlildVc:homeVC title:@"外卖" imageName:@"tab_shopping" selectedImageName:@"tab_shopping_selected"];
        WMFindViewController *findVC=[[WMFindViewController alloc]init];
        self.findVC=[self addOneChlildVc:findVC title:@"发现" imageName:@"tab_discovery" selectedImageName:@"tab_discovery_selected"];
        WMOrderViewController *orderVC=[[WMOrderViewController alloc]init];
        self.orderVC=[self addOneChlildVc:orderVC title:@"订单" imageName:@"tab_order" selectedImageName:@"tab_order_selected"];
        WMMineViewController *mineVC=[[WMMineViewController alloc]init];
        self.mineVC=[self addOneChlildVc:mineVC title:@"我的" imageName:@"tab_user" selectedImageName:@"tab_user_selected"];
        [self setViewControllers:@[self.homeVC,self.findVC,self.orderVC,self.mineVC]];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.delegate = self;
    self.view.backgroundColor = [UIColor whiteColor];
}
- (WMBaseNavViewController *)addOneChlildVc:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName
{
    
    childVc.tabBarItem.title = title;
    [childVc.tabBarItem setTitleTextAttributes:@{
                                                 NSForegroundColorAttributeName:COLOR_RGBA(102, 102, 102, 1),
                                                 NSFontAttributeName: [UIFont fontWithName:SXSFont size:realValue6(20)],
                                                 }                                 forState:UIControlStateNormal];
    childVc.tabBarItem.image = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    NSDictionary *dic = @{
                          NSForegroundColorAttributeName: COLOR_RGBA(255, 82, 79, 1),
                          NSFontAttributeName: [UIFont fontWithName:SXSFont size:realValue6(20)],
                          };
    [childVc.tabBarItem setTitleTextAttributes:dic forState:UIControlStateSelected];
    childVc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    WMBaseNavViewController *nav = [[WMBaseNavViewController alloc] initWithRootViewController:childVc];
    return nav;
}
#pragma mark - 提高tabbar高度
- (void)makeTabBarHigh
{
    CGFloat kTabBarHeight = TABBAR_HEIGHT;
    
    self.tabBar.backgroundColor = [UIColor whiteColor];
    self.tabBar.shadowImage = [[UIImage alloc] init];
    
    self.tabBar.frame = CGRectMake(0, ScreenHeight - kTabBarHeight, ScreenWidth, kTabBarHeight);
    UIView *transitionView = [[self.view subviews] objectAtIndex:0];
    transitionView.height = ScreenHeight - kTabBarHeight;
    
    UIImageView *bgViewNormal = [[UIImageView alloc] init];
    bgViewNormal.backgroundColor = self.tabBar.backgroundColor;
    bgViewNormal.frame = CGRectMake(0, 1, ScreenWidth, kTabBarHeight);
    [self.tabBar insertSubview:bgViewNormal atIndex:0];
    self.tabBar.backgroundImage = [self imageWithColor:[UIColor whiteColor] size:CGSizeMake(ScreenWidth, TABBAR_HEIGHT)];
    self.tabBar.translucent = NO;
}
- (UIImage *)imageWithColor:(UIColor *)color size:(CGSize)size
{
    if (!color || size.width <= 0 || size.height <= 0) return nil;
    CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, color.CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
    return self.selectedViewController.preferredStatusBarStyle;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
